'''
Created on Jul 12, 2011

@author: Cihat Basol
'''

import os

from PyQt4.QtGui import *
from PyQt4.QtCore import SIGNAL, SLOT, QDir, QSettings, QVariant

from dexen.system.client import ui
from dexen.system.common.excepts import DexenInvalidJobDefError, DexenInvalidJobNameError


svr_proxy = ui.svr_proxy


class NewJobDialog(QDialog):
    def __init__(self, parent):
        super(NewJobDialog, self).__init__(parent)
        self.setWindowTitle("Run New Job")
        self.setFixedHeight(150)
        self.setFixedWidth(500)
        self.setModal(True)
        self._create_widgets()
        self._read_settings()   
       
    def _create_widgets(self):
        # Create widgets
        self.jobNameEdit = QLineEdit()
        self.jobDefPathEdit = QLineEdit()
        self.browseBtn = QPushButton("Browse")
        self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | 
                                          QDialogButtonBox.Cancel)
        
        self.jobDefPathEdit.setReadOnly(True)
        
        # Setup layout
        vLayout = QVBoxLayout()
        
        hLayout = QHBoxLayout()
        hLayout.addWidget(self.jobDefPathEdit)
        hLayout.addWidget(self.browseBtn)
         
        fromLayout = QFormLayout()
        fromLayout.addRow("Job Name:", self.jobNameEdit)
        fromLayout.addRow("JobDef Dir:", hLayout)
        
        vLayout.addLayout(fromLayout)
        vLayout.addWidget(self.buttonBox)

        self.setLayout(vLayout)
        
        # Configure signal/slots
        self.connect(self.buttonBox, SIGNAL("accepted()"), self.on_accepted)
        self.connect(self.buttonBox, SIGNAL("rejected()"), self, SLOT("reject()"))
        self.connect(self.browseBtn, SIGNAL("clicked()"), self.on_browseBtn_clicked)
    
    def on_browseBtn_clicked(self):
        print "browse button is clicked"
        rootdir = QFileDialog.getExistingDirectory(self, "Open Directory", 
                                               self._load_rootdir(),
                                               QFileDialog.ShowDirsOnly | 
                                               QFileDialog.DontResolveSymlinks)
        if not os.path.exists(rootdir): return
        self._save_rootdir(rootdir)
        self.jobDefPathEdit.setText(rootdir)
    
    def on_accepted(self):
        job_name = str(self.jobNameEdit.text())
        job_def_dir = str(self.jobDefPathEdit.text())
        try:
            svr_proxy.start_job(job_name, job_def_dir)
        except DexenInvalidJobDefError:
            err_msg = "The specified folder is not a valid job def folder.\n"
            err_msg = "Please select a valid job def folder."
            msg_box = QMessageBox(self)
            msg_box.setText(err_msg)
            msg_box.exec_()
        except DexenInvalidJobNameError, err:
            msg_box = QMessageBox(self)
            msg_box.setText(err.message)
            msg_box.exec_()            
        else:
            self.accept()

    def _read_settings(self):
        self.jobDefPathEdit.setText(self._load_rootdir())

    def _load_rootdir(self):
        settings = QSettings()
        rootdir = settings.value("NewJobDlg/JobDefPath", 
                                 QVariant(QDir.rootPath())).toString()
        if not os.path.exists(rootdir):
            return QDir.rootPath()    
        return rootdir
    
    def _save_rootdir(self, rootdir):
        settings = QSettings()
        rootdir = settings.setValue("NewJobDlg/JobDefPath", rootdir)         
    
    